﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure

Namespace Controllers

    <CustomAuthorize(Roles:=PrivilegeConstants.AdminPrivilege)> _
    Public Class UsersController
        Inherits Controller
        Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository

#If ACS Then
        Private ReadOnly userRepository As IUserRepository

        Public Sub New()
            Me.New(New UserTablesServiceContext(), New UserTablesServiceContext())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal userRepository As IUserRepository)
            Me.userPrivilegesRepository = userPrivilegesRepository
            Me.userRepository = userRepository
        End Sub

        Public Function Index() As ActionResult
            Dim users = Me.userRepository.GetAllUsers().Select(Function(user) New UserPermissionsModel With {.UserName = user.Name, .UserId = user.UserId, .TablesUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.UserId, PrivilegeConstants.TablesUsagePrivilege), .BlobsUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.UserId, PrivilegeConstants.BlobsUsagePrivilege), .QueuesUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.UserId, PrivilegeConstants.QueuesUsagePrivilege), .SqlUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.UserId, PrivilegeConstants.SqlUsagePrivilege)})

            Return Me.View(users)
        End Function
#Else
        Private ReadOnly membershipService As IMembershipService

        Public Sub New()
            Me.New(New UserTablesServiceContext(), New AccountMembershipService())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal membershipService As IMembershipService)
            Me.userPrivilegesRepository = userPrivilegesRepository
            Me.membershipService = membershipService
        End Sub

        Public Function Index() As ActionResult
            Dim users = Me.membershipService.GetAllUsers().Cast(Of MembershipUser)().Select(Function(user) New UserPermissionsModel With {.UserName = user.UserName, .UserId = user.ProviderUserKey.ToString(), .TablesUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.ProviderUserKey.ToString(), PrivilegeConstants.TablesUsagePrivilege), .BlobsUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.ProviderUserKey.ToString(), PrivilegeConstants.BlobsUsagePrivilege), .QueuesUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.ProviderUserKey.ToString(), PrivilegeConstants.QueuesUsagePrivilege), .SqlUsage = Me.userPrivilegesRepository.HasUserPrivilege(user.ProviderUserKey.ToString(), PrivilegeConstants.SqlUsagePrivilege)})

            Return Me.View(users)
        End Function
#End If

        <HttpPost()> _
        Public Sub SetUserPermissions(ByVal userId As String, ByVal useTables As Boolean, ByVal useBlobs As Boolean, ByVal useQueues As Boolean, ByVal useSql As Boolean)
            Me.SetStorageItemUsagePrivilege(useTables, userId, PrivilegeConstants.TablesUsagePrivilege)
            Me.SetStorageItemUsagePrivilege(useBlobs, userId, PrivilegeConstants.BlobsUsagePrivilege)
            Me.SetStorageItemUsagePrivilege(useQueues, userId, PrivilegeConstants.QueuesUsagePrivilege)
            Me.SetStorageItemUsagePrivilege(useSql, userId, PrivilegeConstants.SqlUsagePrivilege)
        End Sub

        Private Sub SetStorageItemUsagePrivilege(ByVal allowAccess As Boolean, ByVal user As String, ByVal privilege As String)
            If allowAccess Then
                Me.userPrivilegesRepository.AddPrivilegeToUser(user, privilege)
            Else
                Me.userPrivilegesRepository.RemovePrivilegeFromUser(user, privilege)
            End If
        End Sub
    End Class
End Namespace